package com.bjpowernode.controller;

import com.bjpowernode.service.CommentService;
import com.bjpowernode.service.ReviewService;
import com.bjpowernode.user.Comment;
import com.bjpowernode.user.Review;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private ReviewService reviewService;

    @RequestMapping(value = "/select.do")
    @ResponseBody
    public Map<Comment,List<Review>> selectComment(Integer id){
        List<Comment> commentList = commentService.findComment(id);
        Iterator<Comment> it = commentList.iterator();
        Map<Comment, List<Review>> crMap = new HashMap<>();
        while(it.hasNext()){
            Comment comment = it.next();
            List<Review> reviewList = reviewService.findReview(comment.getId());
            crMap.put(comment,reviewList);
        }
        return crMap;
    }

    @RequestMapping(value = "/add.do",produces = "application/text;charset=utf-8")
    @ResponseBody
    public String addComment(@RequestParam("username") String username,@RequestParam("comment") String comment,@RequestParam("plid") Integer plid){
        Comment comment1 = new Comment();
        comment1.setUsername(username);
        comment1.setPlneirong(comment);
        comment1.setPlid(plid);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());
        comment1.setPltime(time);
        int res = commentService.addComment(comment1);
        return res == 1 ? "评论成功" : "评论失败";
    }
}
